package com.teach.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teach.entity.ResponseEntity;
import com.teach.entity.TbPower;
import com.teach.mapper.TbUserMapper;
import com.teach.service.TbPowerService;
import com.teach.mapper.TbPowerMapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class TbPowerServiceImpl extends ServiceImpl<TbPowerMapper, TbPower>
    implements TbPowerService{
    @Resource
    private TbPowerMapper tbPowerMapper;
    @Resource
    private TbUserMapper tbUserMapper;

    @Override
    public ResponseEntity getAll() {
        //获取全部的数据
        List<TbPower> powerList = tbPowerMapper.selectList(null);
        //一级节点
        List<TbPower> tbPowerList = powerList.stream().filter(power -> {
            return power.getPid() == 0;
        }).map(power -> {
            //将数据循环放入集合
            power.setTbPowers(getChild(power, powerList));
            return power;
        }).collect(Collectors.toList());
        return new ResponseEntity(tbPowerList,200,"查询成功",true);
    }

    private List<TbPower> getChild(TbPower power, List<TbPower> powerList) {
        List<TbPower> tbPowerList = powerList.stream().filter(tbPower -> {
            //设置二级节点以及三级节点
            return power.getId().intValue() == tbPower.getPid().intValue();
        }).map(tbPower -> {
            tbPower.setTbPowers(getChild(tbPower, powerList));
            return tbPower;
        }).collect(Collectors.toList());
        return tbPowerList;
    }

    @Override
    public ResponseEntity powerSave(TbPower tbPower, String token) {
        Boolean flag=false;
        List<Map> list=new ArrayList<>();
        //解析token
        Claims claims = Jwts.parser().setSigningKey("huangyujie").parseClaimsJws(token).getBody();
        int uid = Integer.parseInt(claims.getId());
        List<Map> mapList = tbUserMapper.selectByUid(uid);
        for (Map map : mapList) {
            //二级名称
            String fname = (String) map.get("fname");
            int indexOf = fname.indexOf("角色管理");
            if(indexOf !=-1){
                //根据登录人的id和二级名称查询三级的名称
                list = tbUserMapper.selectConunt(uid,fname);
            }
        }
        //判断是否有添加的权限
        for (Map map : list) {
            //三级名称
            String gname= (String) map.get("gname");
            //判断是否有添加权限
            if((gname.indexOf("添加"))!=-1){
                flag=true;
            }
        }
        //添加权限
        if(flag){
            tbPowerMapper.insert(tbPower);
        }
        return new ResponseEntity(null,200,"添加成功",true);
    }

    @Override
    public ResponseEntity findById(Integer id) {
        TbPower tbPower = tbPowerMapper.selectById(id);
        return new ResponseEntity(tbPower,200,"回显成功",true);
    }

    @Override
    public ResponseEntity updatePower(TbPower tbPower, String token) {
        Boolean flag=false;
        List<Map> list=new ArrayList<>();
        //解析token
        Claims claims = Jwts.parser().setSigningKey("huangyujie").parseClaimsJws(token).getBody();
        int uid = Integer.parseInt(claims.getId());
        List<Map> mapList = tbUserMapper.selectByUid(uid);
        for (Map map : mapList) {
            //二级名称
            String fname = (String) map.get("fname");
            int indexOf = fname.indexOf("角色管理");
            if(indexOf !=-1){
                //根据登录人的id和二级名称查询三级的名称
                list = tbUserMapper.selectConunt(uid,fname);
            }
        }
        //判断是否有添加的权限
        for (Map map : list) {
            //三级名称
            String gname= (String) map.get("gname");
            //判断是否有添加权限
            if((gname.indexOf("修改"))!=-1){
                flag=true;
            }
        }
        if(flag){
            try {
                tbPowerMapper.updateById(tbPower);
            }catch (Exception e){
                e.printStackTrace();
                return new ResponseEntity(null,500,"修改失败",false);
            }
        }
        return new ResponseEntity(null,200,"修改成功",true);
    }
}




